/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.san21go.jfinfunc.math.var;

import org.san21go.jfinfunc.AbstractFunction;
import org.san21go.jfinfunc.FinFuncRuntimeException;
import org.san21go.jfinfunc.jfinfunc.math.AVERAGE;
/**
 * 估算基于样本的方差<br/>
 * VAR.S 函数<br/>
 * @author xulei
 *
 */
public class VARS extends AbstractFunction<Double> {
	private double[] values;

	public VARS(double[] values) {
		this.values = values;
	}

	@Override
	public Double evaluate() {

		if (values == null || values.length == 0) {
			throw new FinFuncRuntimeException("参数错误");
		}
		if (values.length == 1) {
			return 0D;
		}

		double avg = new AVERAGE(values).evaluate();

		double den = 0;
		for (double value : values) {
			den = den + Math.pow((value - avg), 2);
		}

		// 此处标准偏差的计算使用“n”方法
		return den / (values.length - 1);
	}

}
